import Config, { GoogleCloudProject } from '../config';
import { isWalletAddressInvalid } from './validate';

export const getTokenIcon = (token: string) => {
    return `${Config.getAPI()}/images/coins/` + token + '.png';
};

export const getTotalAmount = (transactions): number => {
    let amount = 0;
    if (transactions.length > 0) {
        for (let i = 0; i < transactions.length; i++) {
            if (!Number.isNaN(amount) && transactions[i].amount > 0) {
                amount += transactions[i].amount;
            }
        }
    }

    return amount;
};

export const getBSCScanUrl = (hash: string): string => {
    const env = Config.getEnvironment();
    return `https://${
        env === GoogleCloudProject.LIVE || env === GoogleCloudProject.INTERNAL ? 'bscscan.com' : 'testnet.bscscan.com'
    }/tx/${hash}`;
};

export const formatWalletAddress = (walletAddress?: string, subStringLength = 7) => {
    if (walletAddress && !isWalletAddressInvalid(walletAddress)) {
        return `${walletAddress.substring(0, subStringLength - 1)}...${walletAddress.substring(
            walletAddress.length - subStringLength,
            walletAddress.length
        )}`;
    }
    return walletAddress || '';
};

export const removeEmpty = (obj) => {
    return Object.fromEntries(Object.entries(obj).filter(([, v]) => v != undefined));
};

export const getUniqueListBy = (arr, key = 'id') => {
    // @ts-ignore
    return [...new Map(arr.map((item) => [get(item, key), item])).values()];
};

